package com.konsung.ksdc.manager.community;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.konsung.ksdc.R;
import com.konsung.ksdc.bean.netbean.RegisterUserNetBean;
import com.konsung.ksdc.common.utils.StringUtils;
import com.konsung.ksdc.config.MyApplication;
import com.squareup.picasso.Picasso;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 创建者     kgh
 * 创建时间   2016/5/27 15:28
 * 更新者     $Author$
 * 更新时间   $Date$
 * 更新描述   ${TODO}
 */
public class GroupMemberGridAdapter extends BaseAdapter {
    private List<RegisterUserNetBean> users;

    /**
     * 构造方法
     * @param users 数据
     */
    public GroupMemberGridAdapter(List<RegisterUserNetBean> users) {
        this.users = users;
    }

    @Override
    public int getCount() {
        return this.users.size();
    }

    @Override
    public Object getItem(int i) {
        return this.users.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup container) {
        RegisterUserNetBean user = this.users.get(position);
        String picPath = StringUtils.appendPicPath(user.getUserPic());
        ViewHolder holder = null;

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(MyApplication.getContext())
                    .inflate(R.layout.item_gridview_group, null);
            ButterKnife.bind(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Picasso.with(container.getContext())
                .load(picPath)
                .placeholder(R.drawable.pic_talkbox_sel)
                .error(R.drawable.pic_talkbox_sel)
                .into(holder.imgContactPic);

        String contactName = "";
        if (StringUtils.isNotEmpty(user.getNickName())) {

            if (user.getNickName().length() > 3) {
                contactName = user.getNickName().substring(0, 3) + "...";
            } else {
                contactName = user.getNickName();
            }
        }

        holder.txtContactNickName.setText(contactName);

        return convertView;
    }

    /**
     * 内部类
     */
    static class ViewHolder {
        @Bind(R.id.img_contact_pic)
        ImageView imgContactPic;
        @Bind(R.id.txt_contact_nick_name)
        TextView txtContactNickName;
    }
}
